package com.jscq.yygh.user.api;

import com.alibaba.excel.util.StringUtils;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.AuthContextHolder;
import com.atguigu.yygh.model.hosp.Order;
import com.atguigu.yygh.model.user.StudentDetail;
import com.atguigu.yygh.vo.hosp.OrderVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jscq.yygh.user.service.OrderService;
import com.jscq.yygh.user.service.StudentDetailService;
import io.swagger.annotations.ApiOperation;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/user/studentDetail")
public class StudentDetailApiController {
    @Autowired
    private StudentDetailService studentDetailService;

    @Autowired
    private OrderService orderService;

    // 逻辑删除
    @ApiOperation(value = "逻辑删除订单信息")
    @DeleteMapping("auth/deleteOrder/{id}")
    public Result deleteOrder(@PathVariable Long id){
        boolean flag = orderService.removeById(id);
        if (flag){
            return Result.ok();
        }else {
            return Result.fail();
        }

    }

    //更新订单状态
    @ApiOperation(value = "更新宿舍上线状态")
    @GetMapping("auth/updateOrderStatus/{id}/{status}")
    public Result updateOrderStatus(@PathVariable String id,
                                            @PathVariable Integer status){

        orderService.updateStatus(id,status);
        return Result.ok();
    }

    @PostMapping("auth/addOrder")
    public Result addOrder(@RequestBody OrderVo orderVo){
        Order order = new Order();
        System.out.println("orderVoTime:"+orderVo.getDate());
        BeanUtils.copyProperties(orderVo,order);
        order.setStatus(0);
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
        order.setIsDeleted(0);
        System.out.println("order:"+order.toString());
        orderService.save(order);
        return Result.ok();
    }


    //条件查询带分页
    @GetMapping("auth/getOrderByStuCode/{current}/{limit}")
    public Result getOrderByStuCode(@PathVariable long current,
                                    @PathVariable long limit,
                                     OrderVo orderVo){
        //创建page对象,传递当前页与记录数
        Page<Order> page=new Page<>(current,limit);
        //构建条件
        QueryWrapper<Order> wrapper=new QueryWrapper<>();
        String stuCode = orderVo.getStudentId();
        String stuName = orderVo.getName();

        if (!StringUtils.isEmpty(stuCode)){
            wrapper.like("student_id",stuCode);
        }
        if (!StringUtils.isEmpty(stuName)){
            wrapper.eq("name",stuName);
        }
        //调用方法实现分页查询

        Page<Order> orderPage = orderService.page(page,wrapper);
        return Result.ok(orderPage);
    }

    //根据stuCode获取学生信息
    @GetMapping("auth/getByStuCode/{stuCode}")
    public StudentDetail getStudentDetailByStuCode(@PathVariable String stuCode){
        StudentDetail studentDetail = studentDetailService.getStudentDetailId(stuCode);
        return studentDetail;
    }

    //获取学生详情
    @GetMapping("auth/findAll")
    public Result findAll(HttpServletRequest request){
        //获取登入用户id
        Long userId = AuthContextHolder.getUserId(request);
        List<StudentDetail> studentDetails =  studentDetailService.findAllUserId(userId);
        return Result.ok(studentDetails);
    }
    //添加学生详情
    @PostMapping("auth/save")
    public Result saveStudentDetail(@RequestBody StudentDetail studentDetail,HttpServletRequest request){
        //获取登入用户id
        Long userId = AuthContextHolder.getUserId(request);
        studentDetail.setUserId(userId);
        studentDetailService.save(studentDetail);
        return Result.ok();
    }
    //根据id获取学生信息
    @GetMapping("auth/get/{id}")
    public Result getStudentDetail(@PathVariable long id){
        StudentDetail studentDetail = studentDetailService.getStudentDetailId(id);
        return Result.ok(studentDetail);
    }
    //修改学生
    @PostMapping("auth/update")
    public Result updateStudentDetail(@RequestBody StudentDetail studentDetail){
        //获取登入用户id
        studentDetailService.updateById(studentDetail);
        return Result.ok();
    }
    //删除学生
    //根据id获取学生信息
    @DeleteMapping("auth/remove/{id}")
    public Result removeStudentDetail(@PathVariable long id){
        studentDetailService.removeById(id);
        return Result.ok();
    }
}
